package com.hzqy.service.statistics;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.hzqy.commons.utils.SystemConfigUtils;
import com.hzqy.persistence.dao.statistics.UserLoginHistoryDao;
import com.hzqy.persistence.dao.statistics.UserPvUvDao;
import com.hzqy.persistence.dao.system.SysConfigDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.vo.StatActiveUserPvUvVo;
import com.hzqy.web.vo.stat.StatUserLoginHistoryVo;
import com.hzqy.web.vo.system.SysConfigVo;

@Service
public class UserPvUvServiceBean extends GenericServiceBean<StatActiveUserPvUvVo, UserPvUvDao, Integer>{
	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private UserPvUvDao userPvUvDao;
	@Resource
	private SysConfigDao sysConfigDao;
	
	@Override
	public UserPvUvDao getEntityDao() {
		return userPvUvDao;
	}
	
	public Map<String,Object> selectUserPvUvList(StatActiveUserPvUvVo statActiveUserPvUvVo, int curPage,int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = userPvUvDao.selectUserPvUvCount(statActiveUserPvUvVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_sdaupu_date", statActiveUserPvUvVo.getF_sdaupu_date());
			map.put("f_sdaupu_area", statActiveUserPvUvVo.getF_sdaupu_area());
			map.put("f_sdaupu_type", statActiveUserPvUvVo.getF_sdaupu_type());
			List<StatActiveUserPvUvVo> results = userPvUvDao.selectUserPvUvList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<StatActiveUserPvUvVo>());
		}
		return map;
	}
	
	public Map<String, Object> uploadFile(MultipartFile uploadify, String filePath){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		CommonsMultipartFile file = (CommonsMultipartFile) uploadify;
		File uploadFile = new File(filePath);
		try {
			LOG.debug("开始上传");
			file.transferTo(uploadFile);
			resultMap.put("flag", 1);
			resultMap.put("msg", "上传成功!");
			LOG.debug("上传完成");
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			String f_sc_value = df.format(new Date());// new Date()为获取当前系统时间
			SysConfigVo sysConfigVo = new SysConfigVo();
			sysConfigVo.setF_sc_key("userList_uploadFile_time");
			sysConfigVo.setF_sc_value(f_sc_value);
			userPvUvDao.updateSystem(sysConfigVo);
			SystemConfigUtils.setSysConfig(SystemConfigUtils.getConfigFromDao());//刷新系统配置缓存
		} catch (Exception e) {
			LOG.error("上传失败", e);
			resultMap.put("flag", -1);
		//	resultMap.put("msg", "上传文件失败： " + e.getMessage());
		}
		
		return resultMap;
	}

}
